import { Sequelize } from 'sequelize';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// 创建SQLite数据库连接
const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: path.join(__dirname, '../../data/accounting.db'),
  logging: process.env.NODE_ENV === 'development' ? console.log : false,
});

// 测试数据库连接
const testConnection = async () => {
  try {
    await sequelize.authenticate();
    console.log('✅ SQLite数据库连接成功');
  } catch (error) {
    console.error('❌ SQLite数据库连接失败:', error);
    process.exit(1);
  }
};

export { sequelize, testConnection };
export default sequelize;